package com.dailyblue.java.spring.boot.redis.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

// 演示String操作
@RestController
@RequestMapping("/string")
public class StringController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/a")
    public String a() {
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        value.set("A", "aaa"); // set A aaa
        value.set("B", "bbb", 10, TimeUnit.SECONDS); // set B bbb ex 10
        value.setIfAbsent("A", "aaa2"); // setnx A aaa2
        value.setIfAbsent("C", "ccc");
        value.set("D", "1");
        value.set("E","eee");
        value.set("F","fff");
        return "";
    }

    @GetMapping("/b")
    public String b() {
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        String v1 = value.get("A");
        String v2 = value.get("B");
        String v3 = value.get("C");
        return v1 + ".." + v2 + ".." + v3;
    }

    @GetMapping("/c")
    public String c() {
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        value.append("A", "999888");
        value.increment("D"); // d+1
        value.increment("D", 3); // d+3
        value.decrement("D"); //d-1
        value.decrement("D", 3);// d-3
        value.getAndExpire("C",1,TimeUnit.MINUTES); // 给C设置1min
        // 删除e和f
        String v1 = value.getAndDelete("E");
        value.set("F","",1,TimeUnit.MILLISECONDS);
        return "";
    }
}
